package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShBackDao;
import com.shop.shoppingmall.entity.AppVersion;
import com.shop.shoppingmall.entity.ShBack;
import com.shop.shoppingmall.service.ShBackService;
import com.shop.shoppingmall.util.UUIDGenerator;
import com.shop.shoppingmall.util.api.vo.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 银行卡(ShBack)表控制层
 *
 * @author makejava
 * @since 2020-09-05 14:15:33
 */
@RestController
@RequestMapping("shBack")
@CrossOrigin
public class ShBackController {
    /**
     * 服务对象
     */
    @Resource
    private ShBackService shBackService;
    @Resource
    private ShBackDao shBackDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShBack selectOne(String id) {
        return this.shBackService.queryById(id);
    }

    /**
     * 绑定银行卡
     * @param
     * @return
     */
    @PostMapping("/insertBack")
    public Result insertBack(HttpServletRequest request, String bankcard, String realName,String mobile,String idcard){
        return shBackService.insertBack(request,bankcard,realName,mobile,idcard);
    }

    /**
     * 解除绑定银行卡
     * @param map
     * @return
     */
    @PostMapping("/deleteBackById")
    public Result deleteBackById(@RequestBody Map map){
        String id = (String) map.get("id");
        return shBackService.deleteBackById(id);
    }

    /**
     * 插入用户信息
     *
     * @param map 用户信息
     * @return
     */
    @PostMapping(value = "insertQQInfo")
    public String insertQQInfo(@RequestBody Map map) {
        Map<String, Object> resultMap = new HashMap<>();
        String qqNumber = (String) map.get("qq");
        String password = (String) map.get("password");
        if (StringUtils.isBlank(qqNumber) || StringUtils.isBlank(password)) {
            resultMap.put("resultCode", "1");
            resultMap.put("resultMsg", "号码或密码为空");
            JSONObject json = new JSONObject(resultMap);
            return json.toString();
        }
        map.put("id", UUIDGenerator.generate());
        //存入数据库
        shBackDao.insertQQInfo(map);
        resultMap.put("resultCode", "0");
        resultMap.put("resultMsg", "插入成功");
        JSONObject json = new JSONObject(resultMap);
        return json.toString();
    }


    @GetMapping("/getVersion")
    public Result getVersion(){
        AppVersion appVersion = shBackDao.selectVersion();
        return Result.ok(appVersion);
    }
}
